﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuatToanDAO;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public partial class Admin_UploadDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if ((Session["UserName"] == null))
        {
            Response.Redirect("Default.aspx?path=" + Request.Url.ToString());
        }
        
        if (!IsPostBack)
        {

            Panel_Edit.Visible = false;

            CategoryDAO cate = new CategoryDAO();
            DataTable dt = cate.ExecuteSelectALL().Tables[0];

            cbbCategoryList.DataSource = dt;
            cbbCategoryList.DataBind();

            CategoryList_Edit.DataSource = dt;
            CategoryList_Edit.DataBind();

            ItemDAO item = new ItemDAO();
            GridView1.DataSource = item.ExecuteSelectFollowCategoryID(cbbCategoryList.SelectedValue.Trim()).Tables[0];
            GridView1.DataBind();

        }
        
        
    }
   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int itemid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
        ItemDAO item = new ItemDAO();
        DataTable dt = item.ExecuteSelectFollowItemID(itemid).Tables[0];

        CategoryList_Edit.SelectedValue = dt.Rows[0]["CategoryID"].ToString();
        txt_EditTitle.Text = dt.Rows[0]["Name"].ToString();
        txtDescription_Edit.Text = dt.Rows[0]["Description"].ToString();
        txtDemo_Edit.Text = dt.Rows[0]["Demo"].ToString();

        if (txtDemo_Edit.Text.Trim() == "")
        {
            btn_Delete.Enabled = false;
        }
        else
        {
            btn_Delete.Enabled = true;
        }
        Panel_Edit.Visible = true;
    }
    
    
    protected void cbbCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string categoryid = cbbCategoryList.SelectedValue.Trim();

        ItemDAO item = new ItemDAO();

        GridView1.DataSource = item.ExecuteSelectFollowCategoryID(categoryid).Tables[0];
        GridView1.DataBind();
        Panel_Edit.Visible = false;
    }

    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        
        try
        {

            if (!FileUpload1.HasFile || !FileUpload2.HasFile)
            {
                lbl_Message.Text = "2 files to upload unselected";
                return;
            }

            //Upload File to server
            string sFolderPath = Server.MapPath(@"..\UserControls\ThuatToan");
            
            //Upload File Control (.ascx)
            HttpPostedFile myFile = FileUpload1.PostedFile;
            string sFileName = myFile.FileName;
            myFile.SaveAs(
                string.Format(@"{0}\{1}", sFolderPath, sFileName));

            //Upload File C# (.cs)
            HttpPostedFile myFileCode = FileUpload2.PostedFile;
            string sFileNameCode = myFileCode.FileName;
            myFileCode.SaveAs(
                string.Format(@"{0}\{1}", sFolderPath, sFileNameCode));

            
            // Updata to DB
            int itemid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
            ItemDAO item = new ItemDAO();
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("ItemID", itemid));
            parameters.Add(new SqlParameter("CategoryID", CategoryList_Edit.SelectedValue));
            parameters.Add(new SqlParameter("Name", txt_EditTitle.Text));
            parameters.Add(new SqlParameter("Description", txtDescription_Edit.Text));
            parameters.Add(new SqlParameter("Demo", sFileName));
            parameters.Add(new SqlParameter("Other1", ""));
            parameters.Add(new SqlParameter("Other2", ""));

            item.ExecuteUpdate(parameters);
            //cbbCategoryList.SelectedValue = CategoryList_Edit.SelectedValue;
            cbbCategoryList_SelectedIndexChanged(null, null);

            lbl_Message.Text = "Upload successfully!";
            txtDemo_Edit.Text = sFileName;

        }
        catch (Exception ex)
        {
            lbl_Message.Text = "Upload failed!";
        }
        
    }


    protected void btn_Delete_Click(object sender, EventArgs e)
    {

        try
        {
            //Delete File from server
            string sFolderPath = Server.MapPath(@"..\UserControls\ThuatToan");
            
            // Delete Control File (.ascx)
            string sFileName = GridView1.SelectedRow.Cells[3].Text;
            string filepath = string.Format(@"{0}\{1}", sFolderPath, sFileName);
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            else
            {
                lbl_Message.Text = "File Control (.ascx) is not exist";

                return;
            }

            // Delete C# File (.cs)
            string sFileNameCode = GridView1.SelectedRow.Cells[3].Text + ".cs";
            string filepathCode = string.Format(@"{0}\{1}", sFolderPath, sFileNameCode);
            if (File.Exists(filepathCode))
            {
                File.Delete(filepathCode);
            }
            else
            {
                lbl_Message.Text = "File C# (.cs) is not exist";

                return;
            }

            // Updata to DB
            int itemid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
            ItemDAO item = new ItemDAO();
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("ItemID", itemid));
            parameters.Add(new SqlParameter("CategoryID", CategoryList_Edit.SelectedValue));
            parameters.Add(new SqlParameter("Name", txt_EditTitle.Text));
            parameters.Add(new SqlParameter("Description", txtDescription_Edit.Text));
            parameters.Add(new SqlParameter("Demo", ""));
            parameters.Add(new SqlParameter("Other1", ""));
            parameters.Add(new SqlParameter("Other2", ""));

            item.ExecuteUpdate(parameters);
            //cbbCategoryList.SelectedValue = CategoryList_Edit.SelectedValue;
            cbbCategoryList_SelectedIndexChanged(null, null);

            txtDemo_Edit.Text = "";
            lbl_Message.Text = "Delete successfully!";
        }
        catch (Exception ex)
        {
            lbl_Message.Text = "Delete failed!";
        }
        
    }
}